//
//  RecoverViewController.m
//  YYZB
//
//  Created by BY_R on 16/3/28.
//
//

#import "RecoverViewController.h"
#import "UserWebService.h"
#import "NSString+MD5.h"
#import "NSString+MobileNo.h"
#import "LoadingView.h"
#import "Button.h"
#import "UserInfoController.h"

#define TotalTimerCount 60
@interface RecoverViewController ()
@property (nonatomic, strong) UserWebService *userWebService;
@property(nonatomic,copy)NSString * numId;
@property (strong, nonatomic) NSTimer *timer;
@property (nonatomic) NSUInteger timerCount;

@property (weak, nonatomic) IBOutlet Button *getVerifyCode;
@end

@implementation RecoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.userWebService = UserWebService.new;
}
- (IBAction)getCode:(id)sender {
    if ([self.phoneTF.text isMobileNumber]) {
        [self.userWebService getVerifyCode:self.phoneTF.text flag:@"100" completionHanlder:^(NSArray *items, NSError *error) {
            
            self.numId = [items valueForKey:@"VerifyCodeId"];
            if (error) {
                [LoadingView showToast:[error localizedDescription]];
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTicked:) userInfo:nil repeats:YES];
                    self.timerCount = TotalTimerCount;
                });
            }
            
        }];
    }
    else{
        [LoadingView showToast:@"手机号有误"];
    }
}
- (IBAction)setNewPsw:(id)sender {
    if ([self.phoneTF.text isMobileNumber] && self.codeTF.text.length!=0 && self.pswTF.text.length>=6 && self.pswTF.text.length<=16 && self.numId.length!=0) {
        
        [self.userWebService forgetPsw:self.phoneTF.text VerifyCode:self.codeTF.text password:[self.pswTF.text MD5] VerifyCodeId:self.numId completionHanlder:^(NSArray *items, NSError *error) {
            if (error) {
                [LoadingView showToast:[error localizedDescription]];
                
            }
            else {
                [LoadingView showToast:@"修改成功！"];
                [UserInfoController setUserNo:self.phoneTF.text];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                });
            }
            
        }
        ];
    }
    else{
        [LoadingView showToast:@"验证码不正确"];
    }
}


- (void)timerTicked:(NSTimer *)timer {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.timerCount -= 1;
        if (self.timerCount == 0) {
            self.timerCount = TotalTimerCount;
            self.getVerifyCode.enabled = YES;
            self.getVerifyCode.title = @"获取验证码";
            [self.timer invalidate];
            self.timer = nil;
        } else {
            self.getVerifyCode.title = [NSString stringWithFormat:@"%@秒", @(self.timerCount)];
            self.getVerifyCode.enabled = NO;
        }
    });
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
